home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
pascal
/
ooptut34.zip
/
TP
/
OOPTUTOR
/
VMT.TXT
< prev
next >
Wrap
Text File
|
1993-06-12
|
4KB
|
98 lines
VIRTUAL METHODS AND THE VIRTUAL METHOD TABLE.
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
Every object type, that contains or inherits a virtual method, has a
Virtual Method Table (VMT) in the data segment. The VMT contains the size
of the object type and a set of pointers to the code for each of its
virtual methods.
For each instance of an object type, the CONSTRUCTOR establishes a link
between that instance and the VMT for that object type. This link is a
16-bit field, called the virtual method table field, which follows the
ordinary data fields in the object type.
There is only one VMT for each object type. Individual instances of an
object type contain a link to the VMT - they do not contain the VMT itself.
Example:
type
Location = object
X, Y: Integer;
procedure Init(NewX, NewY: Integer);
function GetX: Integer;
function GetY: Integer;
end;
PointPtr = ^Point;
Point = object(Location);
Color: Integer;
constructor Init(NewX, NewY, NewColor: Integer);
destructor Done; virtual;
procedure Show; virtual;
procedure Hide; virtual;
procedure MoveTo(NewX, NewY: Integer); virtual;
end;
CirclePtr = ^Circle;
Circle = object(Point);
Radius: Integer;
constructor Init(NewX, NewY, NewColor, NewRadius: Integer);
procedure Show; virtual;
procedure Hide; virtual;
procedure Fill: virtual;
end;
Instances of types Location, Point and Circle are shown below:
Location Point Circle
----------------- ----------------- -----------------
| X | | X | | X |
----------------- ----------------- -----------------
| Y | | Y | | Y |
----------------- ----------------- -----------------
| Color | | Color |
----------------- -----------------
| VMT | | VMT |
----------------- -----------------
| Radius |
-----------------
Because Point is the first type in the hierarchy that introduces virtual
methods, the VMT field is allocated immediately after the Color field.
VMTs are built automatically by the compiler and are never directly
manipulated by a program. Likewise, pointers to VMTs are automatically
stored in object type instances by the object type's constructors and are
never directly manipulated by the program.
The layouts of the VMTs for point and circle types are shown below:
Point VMT Circle VMT
-------------------- --------------------- ---------------------
| $0008 | | $000A | Size
-------------------- --------------------- ---------------------
| $FFF8 | | $FFF6 | Negative size (check)
-------------------- --------------------- ---------------------
| @Point.Done | | @Point.Done | Seg:Ofs addresses of
-------------------- ---------------------
| @Point.Show | | @Circle.Show | methods, inherited
-------------------- ---------------------
| @Point.Hide | | @Circle.Hide | like Done and MoveTo
-------------------- ---------------------
| @Point.MoveTo | | @Point.MoveTo | or overridden like
-------------------- ---------------------
| @Circle.Fill | Show and Hide.
--------------------- ---------------------
Circle inherits Done and MoveTo methods from Point, but overrides Show and
Hide methods and has its own new method Fill.
VMT.TXT
22.8.92
1.6.93